import dayjs from "dayjs";
import {Component, h} from "vue";
import {NIcon} from "naive-ui";

export const renderIcon = (icon: Component, props = {size: "12px"}) => {
    return () => h(NIcon, props, {default: () => h(icon)});
}

export const isNullOrUndefined = (val: any): boolean => {
    return val === null || typeof val === "undefined" || val === "null";
}

export const isEmpty = (val: any): boolean => {
    return isNullOrUndefined(val) || val === "";
}

export const formatDateTime = (dateTime = undefined as dayjs.ConfigType, format = "YYYY-MM-DD HH:mm:ss"): string => {
    return dayjs(dateTime).format(format);
}

export const timePlusZero = (time: number | undefined): string => {
    if (time === 0) {
        return "00";
    }
    if (!time) {
        return "";
    }
    return time > 9 ? "" + time : "0" + time;
}

export const dayStart = (): number => {
    return dayjs().startOf("day").valueOf();
}

export const nextDayStart = (): number => {
    return dayjs().add(1, "day").startOf("day").valueOf();
}

export const isJSON = (str: string): boolean => {
    try {
        const obj = JSON.parse(str);
        return !!(typeof obj === "object" && obj);
    } catch (e) {
        return false;
    }
}
